package com.spring.authManage.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.spring.authManage.dao.RoleInfoDao;
import com.spring.authManage.pojo.RoleInfo;
import com.spring.authManage.pojo.RoleInfoParam;
import com.spring.common.dao.MySqlBaseDao;

@Repository("roleInfoDao")
public class RoleInfoDaoImpl extends MySqlBaseDao implements RoleInfoDao{

	/**
	 * 新增角色信息
	 * @param roleInfo
	 */
	public void addRoleInfo(RoleInfo roleInfo) throws Exception {
		this.getSqlMapClientTemplate().insert("addRoleInfo",roleInfo);
	}
	/**
	 * 修改角色信息
	 * @param roleInfo
	 */
	public void updateRoleInfo(RoleInfo roleInfo) throws Exception {
		this.getSqlMapClientTemplate().update("updateRoleInfo",roleInfo);
	}
	/**
	 * 根据角色ID获取角色信息
	 * @param id
	 * @return
	 */
	public RoleInfo getRoleInfoById(String id) throws Exception {
		return (RoleInfo) this.getSqlMapClientTemplate().queryForObject("getRoleInfoById",id);
	}
	/**
	 * 根据用户ID 获取该用户所有角色 
	 * @param userId
	 * @return
	 */
	public List<RoleInfo> getRoleInfosByUser(String userId) throws Exception {
		List<RoleInfo> roleInfoList = this.getSqlMapClientTemplate().queryForList("getRoleInfosByUser",userId);
		return roleInfoList;
	}
	/**
	 * 根据查询条件获取角色信息
	 * @param param
	 * @return
	 */
	public RoleInfo queryRoles(RoleInfo roleInfo) throws Exception {
		int totalRecord=0;
		if (roleInfo.pageResult.getIsPaging()) {
			Object obj = this.getSqlMapClientTemplate().queryForObject("queryRolesCount", roleInfo);
			totalRecord = Integer.parseInt(obj.toString());
			roleInfo.pageResult.setTotalRecord(totalRecord);
		}
		if (totalRecord > 0 || !roleInfo.pageResult.getIsPaging()) 
		{
			roleInfo.pageResult.setList(this.getSqlMapClientTemplate().queryForList("queryRoles",roleInfo));
		}
		return roleInfo;
	}
	/**
	 * 删除角色
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public void updateRoleById(String id) throws Exception {
		this.getSqlMapClientTemplate().update("updateRoleById", id);
	}
	/**
	 * 重启删除角色
	 * @param roleId
	 */
	public void restartRoleById(String roleId) throws Exception {
		this.getSqlMapClientTemplate().update("restartRoleById",roleId);
	}
	/**
	 * 删除某角色所有权限
	 * @param roleId
	 */
	public void deleteRoleResources(String roleId) throws Exception {
		this.getSqlMapClientTemplate().delete("deleteRoleResources",roleId);
	}
	/**
	 * 是否存在角色
	 * @param roleInfo
	 * @return
	 */
	public Integer isExistRole(RoleInfo roleInfo) throws Exception {
		return (Integer) this.getSqlMapClientTemplate().queryForObject("isExistRole",roleInfo);
	}
	/**
	 * 添加(保存)角色权限
	 * @param infos
	 */
	@Override
	@Transactional(rollbackFor = Exception.class, value = "transactionManager")
	@SuppressWarnings("unchecked")
	public void updateRoleResources(final List<RoleInfo> list) throws Exception {
		if (list != null && list.size()>0) {
			this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() {		//再插入
				public Object doInSqlMapClient(SqlMapExecutor executor)
						throws SQLException {
					executor.startBatch();
					for (int i = 0; i < list.size(); i++) {
						executor.insert("updateRoleResources", list.get(i));
					}
					return executor.executeBatch();
				}
			});
		}
	}
	
	/**
	 * 根据权限名称查询所有拥有该权限的角色
	 * @param resourceName
	 * @return
	 */
	public List<RoleInfo> getRoleInfosByResourceName(String resourceName) throws Exception {
		List<RoleInfo> roleInfoList = this.getSqlMapClientTemplate().queryForList("getRoleInfosByResourceName",resourceName);
		return null;	
	}
	/**
	 * 根据查询条件获取角色信息 不分页
	 * @param param
	 * @return
	 */
	public List<RoleInfo> queryAllRoleInfoByParam(RoleInfoParam roleInfoParam) throws Exception {
		List<RoleInfo> roleInfoList = this.getSqlMapClientTemplate().queryForList("queryAllRoleInfoByParam",roleInfoParam);
		return roleInfoList;
	}
	/**
	 * 获取角色权限
	 * 
	 * @param request
	 * @param response
	 * @param roleId
	 */
	public List<RoleInfo> getRoleResource(String id) throws Exception{
		return this.getSqlMapClientTemplate().queryForList("getRoleResource",id);
	}
}
